//
//  MineSetController.swift
//  HappyEat
//
//  Created by ctx2016 on 17/1/11.
//  Copyright © 2017年 陌路是我. All rights reserved.
//

import UIKit

class MineSetController: UIViewController ,UITableViewDelegate,UITableViewDataSource{
    
    let userDefault:UserDefaults = UserDefaults.standard
    
    let CellId = "CellId"
    
    var footView:UIView = UIView.init(frame: CGRect.init(x: 0, y: 0, width: k_Wide, height: 64))
    
    var tableView:UITableView = UITableView.init(frame: .zero, style: .plain)
    let titleNames:[String] = ["消息通知","清除缓存","意见反馈","关于我们"]
    override func viewDidLoad() {
        super.viewDidLoad()
        self.createTableView()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        self.setNavi()
        UIApplication.shared.statusBarStyle = .default
    }
    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
    }
}
extension MineSetController {
    //MARK:设置导航栏选项
    func setNavi(){
        self.navigationItem.title = "设置"
        self.navigationController?.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName:UIColor.black]
        self.navigationItem.leftBarButtonItem = UIBarButtonItem.init(title: "", imagStr: "ic_back", fontSize: 14, target: self, action: #selector(leftBarButtonAction))
    }
    //MARK:CreateTableView
    func createTableView(){
        self.view.addSubview(self.tableView)
        self.tableView.snp.makeConstraints { (make) in
            make.left.equalTo(0)
            make.right.equalTo(0)
            make.top.equalTo(0)
            make.bottom.equalTo(0)
        }
        
        self.tableView.delegate = self
        self.tableView.dataSource = self
        self.tableView.register(MineSetSwitchCell.self , forCellReuseIdentifier: CellId)
        tableView.separatorStyle = .none
        self.tableView.tableFooterView = self.footView
        let cancleLoginButton:UIButton = UIButton.init()
        self.footView.addSubview(cancleLoginButton)
        cancleLoginButton.snp.makeConstraints { (make) in
            make.left.equalTo(16)
            make.right.equalTo(-16)
            make.top.equalTo(12)
            make.height.equalTo(40)
        }
        cancleLoginButton.setTitle("退出登录", for: .normal)
        cancleLoginButton.setTitleColor(.white, for: .normal)
        cancleLoginButton.backgroundColor = .red
        cancleLoginButton.titleLabel?.font = UIFont.systemFont(ofSize: 16)
        cancleLoginButton.addTarget(self, action: #selector(cancleButtonAction), for: .touchUpInside)
    }
    //MARK:CancleButtonAction
    func cancleButtonAction(){
        //退出登录事件)
        self.userDefault.removeObject(forKey: ID)
        self.userDefault.removeObject(forKey: PHONE)
        self.userDefault.removeObject(forKey: PASSWWORD)
        self.userDefault.removeObject(forKey: NICKNAME)
        self.userDefault.removeObject(forKey: HEADIMGE)
        self.userDefault.removeObject(forKey: REGISTTIME)
        self.userDefault.removeObject(forKey: LASTLOGINTIME)
        self.userDefault.removeObject(forKey: STATUS)
        self.userDefault.removeObject(forKey: BALANCE)
        self.userDefault.removeObject(forKey: POINTS)
        self.userDefault.removeObject(forKey: INVITECODE)
        self.userDefault.removeObject(forKey: VERIFYCODE)
        self.userDefault.synchronize()
        self.navigationController?.popToRootViewController(animated: true)
    }
    //MARK:LeftBarButtonAction
    func leftBarButtonAction(){
        self.navigationController!.popViewController(animated: false)
    }
    //MARK:tableviewDelegate
    @objc(tableView:didSelectRowAtIndexPath:) func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        switch indexPath.row {
        case 0:
            break
        case 1:
            break
        case 2:
            let yijianVC:MineSetYiJianController = MineSetYiJianController()
            self.navigationController?.pushViewController(yijianVC, animated: true)
            break
        default:
            break
        }
    }
    @objc(tableView:heightForRowAtIndexPath:) func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 50
    }
    @objc(numberOfSectionsInTableView:) func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }
    @objc(tableView:cellForRowAtIndexPath:) func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        switch indexPath.row {
        case 0:
            //Switch
//            let cell = MineSetSwitchCell.init(style: .default, reuserIdentify: CellId, cellStyle: .switchButtonStyle)
            let cell = tableView.dequeueReusableCell(withIdentifier: CellId)as!MineSetSwitchCell
            cell.cellStyle = .switchButtonStyle
            cell.leftLabel.text = titleNames[indexPath.row]
            cell.selectionStyle = .none
            return cell
        case 1:
            let cell = tableView.dequeueReusableCell(withIdentifier: CellId)as!MineSetSwitchCell
            cell.cellStyle = .noRightIconStyle
            cell.rightLabel.text = "0KB"
            cell.leftLabel.text = titleNames[indexPath.row]
            cell.selectionStyle = .none
            return cell
        default:
            let cell = tableView.dequeueReusableCell(withIdentifier: CellId)as!MineSetSwitchCell
            cell.cellStyle = .rightAllStyle
            cell.leftLabel.text = titleNames[indexPath.row]
            cell.selectionStyle = .none
            return cell
        }
    }
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 4
    }
}
